
//COMPUTE MEASURES OF BORROWER EXPOSURES TO EACH BANK IN THE SAMPLE

* Get underlying loan data 
use ../Intermediate/transaction_clean, clear
joinby transaction_id using ../Intermediate/transaction_lender_clean
drop if missing(BankID) | missing(date_trans) | obs_ammendment == 1 | ///
				amount_trans_usdm <= 0 | missing(amount_trans_usdm) | ///
				amount_trans_lender_usdm < 0
replace lead_bank = 1 if bond_ind == 1 
egen num_lead = sum(lead_bank), by(borrower_id transaction_id)
gen amount_lead_usdm = amount_trans_usdm/num_lead if bond_ind == 0  
replace amount_lead_usdm = amount_trans_lender_usdm if bond_ind == 1
drop num_lead

//scopes of interest:
gen dtype_debt = 1 
gen period_pre  = inrange(date_trans,d(01jan2009),d(01jan2014)) 
gen rtype_all = amount_trans_lender_usdm 

//Create exposure share variables
egen sum_period_all_debt  = sum(period_pre * rtype_all * dtype_debt), by(borrower_id)
egen bank_period_all_debt = sum(period_pre * rtype_all * dtype_debt), by(borrower_id BankID)
gen  exp_share_pre_all_debt = bank_period_all_debt / sum_period_all_debt
drop sum_period_all_debt bank_period_all_debt


sort borrower_id BankID date_trans transaction_id 
keep borrower_id BankID exp_share_*
duplicates drop

foreach exp of var exp_share_* {
	replace `exp' = 0 if missing(`exp')
}


compress

save ../Intermediate/exposure_shares, replace
